//
//  EFSArticleViewController.m
//  EFSMobile
//
//  Created by Chuan on 12/03/15.
//  Copyright © 2015 Elephants Financial Service. All rights reserved.
//

#import "EFSArticleViewController.h"
#import "WXApi.h"
#import "YSUserManager.h"
//#import "EFSUserManager.h"
//#import "EFSServiceManager.h"
#import "EFSShareManager.h"

#import "EFSWKWebViewController+CommonEvent.h"
#import "NSURL+YTX.h"
#import "TSMessage+YTX.h"

#import <YTXRequest/YTXRequest.h>


@implementation EFSArticleViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadArticleInfo];
    [self handlerNavigate];

    if ([WXApi isWXAppInstalled]) {
        [self setupShareButton];
    }

    self.isShowProgress = YES;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

//    self.navigationController.navigationBar.barTintColor = [UIColor navigationBarColor];
//    [self.navigationController.navigationBar setTitleTextAttributes:
//     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


- (NSString *)mapShareTypeStr
{
    NSString *type = @"news";
    return type;
}

- (void)loadArticleInfo
{
    if (self.articleInfo) {
        
        self.url = [NSURL URLWithString:[self urlForArticle]];
        
        NSString *viewTitle = [self.articleInfo valueForKey:@"viewTitle"];
        NSArray *columns = self.articleInfo[@"columns"];
        NSDictionary *dic = columns[0];
        self.title = viewTitle ? viewTitle : [self.articleInfo valueForKey:@"category"];
        if ([dic[@"id"] isEqual:@134]){
            self.title = @"公告中心";
        }else if([dic[@"id"] isEqual:@130]){
            self.title = @"钱币咨询";
        }
        //        self.title = [self.articleInfo valueForKey:@"category"] ?: [[[self.articleInfo valueForKey:@"tags"] lastObject] valueForKey:@"name"];
        //[self refreshWithData:self.articleInfo];
    }
}

- (NSString *)urlForArticle
{
    
    NSURL *articleShareUrl  = [YSRequest urlWithName:@"aggregate.articlesdetail"];
    articleShareUrl = [articleShareUrl URLByAppendingEnvAgentAndToken];
    articleShareUrl = [articleShareUrl URLByAppendingQueryDictionary:@{@"id": objectOrEmptyStr(self.articleInfo[@"id"]),
//                                                                       @"tradeAccount": objectOrEmptyStr([EFSUserManager sharedManager].currentTradeAccount.account),
//                                                                       @"userType": objectOrEmptyStr([EFSUserManager sharedManager].currentUser.userType),
                                                                       @"userType": @"0",
                                                                       @"share":@"true",
//                                                                       @"appName":@"ygmobile",
                                                                       @"platform":@"ios"}];
    return [articleShareUrl absoluteString];
}

#pragma mark share
- (void)setupShareButton
{
    
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(showActionSheet:)];
    self.navigationItem.rightBarButtonItem = shareButton;
}

- (void)showActionSheet:(UIBarButtonItem*)button{
    
    @weakify(self)
    NSDictionary *shareInfo = @{
                                @"id": objectOrEmptyStr(self.articleInfo[@"id"]),
                                @"type": [self mapShareTypeStr],
                                @"image": objectOrEmptyStr(self.articleInfo[@"img"]),
                                @"title": objectOrEmptyStr(self.articleInfo[@"title"]),
                                @"url": objectOrEmptyStr([self urlForArticle]),
                                @"content": objectOrEmptyStr(self.articleInfo[@"desc"])
                                };
    [[EFSShareManager shareContentWithInfo:shareInfo] subscribeNext:^(id x) {
        @strongify(self)
        [TSMessage ytx_showNotificationInViewController:self title:x[@"message"] type:TSMessageNotificationTypeSuccess];
    } error:^(NSError *error) {
        @strongify(self)
        [TSMessage ytx_showNotificationInViewController:self title:[error.userInfo valueForKey:EFS_ERROR_MESSAGE_KEY]
                                                   type:TSMessageNotificationTypeError];
    }];
}

- (RACSignal *)showArticleByArticleId: (NSString *)articleId
{
    NSURL *articleShareUrl  = [YSRequest urlWithName:@"aggregate.articlesdetail"];
    articleShareUrl = [articleShareUrl URLByAppendingEnvAgentAndToken];
    articleShareUrl = [articleShareUrl URLByAppendingQueryDictionary:@{@"id": objectOrEmptyStr(articleId),
                                                                       //                                                                       @"tradeAccount": objectOrEmptyStr([EFSUserManager sharedManager].currentTradeAccount.account),
                                                                       //                                                                       @"userType": objectOrEmptyStr([EFSUserManager sharedManager].currentUser.userType),
                                                                       @"userType": @"0",
                                                                       @"share":@"true",
                                                                       //                                                                       @"appName":@"ygmobile",
                                                                       @"platform":@"ios"}];
    self.url = [NSURL URLWithString:[articleShareUrl absoluteString]];
    @weakify(self)
    RACReplaySubject *subject = [RACReplaySubject subject];
    YSRequest *request = [YSRequest requestWithName:@"article.articleById"];
    NSDictionary *params = @{@"serverId": @"3004",
                             @"token": objectOrEmptyStr([YSUserManager sharedManager].currentUser.token),
                             @"ids": objectOrEmptyStr(articleId)
                             };
    [request sendWithParameters:params success:^(id response) {
        @strongify(self)
//        EFSLogH(request.requestUrl, request.requestMethod, params, [response[@"articles"] firstObject], nil);
        self.articleInfo = [response[@"articles"] firstObject];
        [self loadArticleInfo];
        [subject sendNext:self.articleInfo];
        [subject sendCompleted];
    } failure:^(YSRequest *request) {
        NSError *error = [NSError errorWithDomain:@"EFS" code:0 userInfo:@{EFS_ERROR_MESSAGE_KEY:@"活动信息获取失败"}];
//        EFSLogH(request.requestUrl, request.requestMethod, params, nil, error);
    }];
    return subject;
    return [RACReplaySubject subject];
}
@end
